<?php
/**
 * 商品 、 商品分类
 */

namespace app\api\model;


use app\common\model\Model;

class Goods extends Model
{
    const TABLE_NAME = 'goods';
    const CACHE_EXPIRE_LEVEL = self::CACHE_EXPIRE_LEVEL_4;

    protected $name = self::TABLE_NAME;

    const GOODS_FIELDS= 'goods_id,goods_name,goods_sn,goods_type,goods_cate,price,formal_price,stock,goods_sell_num,goods_img,describe,comment_num,goods_points';
    const GOODS_POINTS_NUM = 500;
    const GOODS_IMAGE_PATH='goods';
    const GOODS_WEIXIN_PUB =1;
    const GOODS_PC_PLAT =2;
    private static function getGoodsListQuery($good_cate=0,$goods_plat=0){

        $query = self::commonQuery(self::TABLE_NAME)
            ->field(self::GOODS_FIELDS)
            ->where(['is_show'=>1,'is_del'=>0]);

        if($good_cate > 0)
        {
            $query->where(['goods_cate'=>$good_cate]);
        }
        if($goods_plat >0){
            //$query->whereExp('goods_plat'," & $goods_plat");
            $query->where(['goods_plat'=>$goods_plat]);
        }
        return $query;
    }
    public static function getGoodsList($good_cate =0,$page=1,$goods_plat=0)
    {
        return self::getGoodsListQuery($good_cate,$goods_plat)
               ->page($page,self::LIMIT)
               ->order('goods_id')
               ->select();
    }
    public static function getGoodsCateList($cate_plat=0)
    {
        $query = self::commonQuery('goods_cate_list');

        if($cate_plat > 0){
            //$query->whereExp('cate_plat'," & $cate_plat");
            $query->where(['cate_plat'=>$cate_plat]);
        }

        return $query ->order('cate_order')->select();
    }

    public static function getGoodsCateName($good_cate_id)
    {
        $info = self::getGoodsCateInfo($good_cate_id);
        return empty($info)?'':$info['cate_name'];
    }

    public static function getGoodsCateInfo($good_cate_id){
        $query = self::commonQuery('goods_cate_list',self::CACHE_EXPIRE_LEVEL_4);
        return  $query->where(['cate_id'=>$good_cate_id])->find();
    }

    public static function getGoodsInfo($goods_id,$cache=false)
    {
        return self::commonQuery(self::TABLE_NAME,$cache)
               ->field(self::GOODS_FIELDS)
               ->where(['goods_id'=>$goods_id])
               ->find();
    }
    public static function handleGoodsInfo($info,$user_id)
    {
        $info['goods_img']=get_image_url(self::GOODS_IMAGE_PATH,$info['goods_img']);

        $info['goods_cate']=self::getGoodsCateName($info['goods_cate']);
        return $info;
    }
    public static function handleGoodsList($list,$user_id)
    {
        $res = [];
        if(empty($list))
        {
            return $res;
        }
        foreach ($list as $key=>$val)
        {
            $res[$key]=self::handleGoodsInfo($val,$user_id);
        }
        return $res;
    }
    public static function incrGoodsCommentNum($goods_id)
    {
        self::commonQuery(self::TABLE_NAME)
            ->where(['goods_id'=>$goods_id])
            ->setInc('comment_num');
    }
    public static function descGoodsCommentNum($goods_id)
    {
        self::commonQuery(self::TABLE_NAME)
            ->where(['goods_id'=>$goods_id,'comment_num'=>['gt',0]])
            ->setDec('comment_num');
    }

   public static function getGoodsPoints($goods_id)
   {
       $info =self::getGoodsInfo($goods_id,self::CACHE_EXPIRE_LEVEL_1);
       return empty($info)?0:$info['goods_points'];
       //return self::GOODS_POINTS_NUM;
   }
    //预定时 ，查询 库存量
    public static function getGoodsInfoForUpdate($goods_id)
    {
        $sql ="select price,stock from goods where goods_id = $goods_id for update";

        $res = self::query($sql);

       
        return empty($res)?[]:$res[0];
    }
    //添加卖出量 和 减少库存量
    public static function descGoodsNum($goods_id,$num)
    {
        return self::commonQuery(self::TABLE_NAME)
            ->where(['goods_id'=>$goods_id])
            ->exp("goods_sell_num","goods_sell_num + $num")
            ->setDec('stock',$num);
    }
}